namespace Brjnk.GpxExplorer.Data.GPX
{
    using System;
    using Brjnk.Common;
    using Brjnk.GpxExplorer.Data.GPX.SerializationClasses;

    /// <summary>
    /// </summary>
    public class GpxRoutePoint : RoutePoint, IGpxObject
    {
        private wptType data;

        public GpxRoutePoint(wptType data)
        {
            this.data = data;
        }

        public GpxRoutePoint() : this(new wptType()) { }

        public object GetUnderlayingObject()
        {
            return data;
        }

        public override Common.GpsCoordinates Position
        {
            get
            {
                return new Common.GpsCoordinates((double)data.lat, (double)data.lon);
            }
            set
            {
                data.lat = (decimal)value.LatTotalDegree;
                data.lon = (decimal)value.LonTotalDegree;
                OnPropertyChanged("Position");
            }
        }

        public override DateTime? Time
        {
            get
            {
                if (data.timeSpecified == false) return null;
                return data.time.ToLocalTime();
            }
            set
            {
                if (value.HasValue)
                {
                    data.time = value.Value.ToUniversalTime();
                    data.timeSpecified = true;
                }
                else
                {
                    data.time = default(DateTime).ToUniversalTime();
                    data.timeSpecified = false;
                }
                OnPropertyChanged("Time");
            }
        }

        public override Distance? Elevation
        {
            get
            {
                if (data.eleSpecified == false) return null;
                return (double)data.ele;
            }
            set
            {
                if (value.HasValue)
                {
                    data.ele = (decimal)(double)value;
                    data.eleSpecified = true;
                }
                else
                {
                    data.ele = default(decimal);
                    data.eleSpecified = false;
                }
                OnPropertyChanged("Elevation");
            }
        }

        public override object Clone()
        {
            return new GpxRoutePoint((wptType)DeepClone.Clone(this.GetUnderlayingObject()));
        }
    }
}